package com.db.protocol.message;

import com.db.protocol.exception.NotParsingValueProtocolException;
import com.db.protocol.exception.ParseProtocolException;
import com.db.protocol.exception.TagMissingProtocolException;
import com.db.protocol.tags.MsgType;
import com.db.protocol.tags.Tag;

import java.util.Map;

/**
 * @author nulls.narik
 */
public class LogonClientMessage extends AbstractMessage {

    public LogonClientMessage(Map<Tag, String> message) throws ParseProtocolException {
        put(Tag.MSG_TYPE, MsgType.LOGON);

        if (!message.containsKey(Tag.CLIENT_ID)) {
             throw new TagMissingProtocolException(Tag.CLIENT_ID);
        }
        put(Tag.CLIENT_ID, message.get(Tag.CLIENT_ID));

        if (!message.containsKey(Tag.TYPE_AGGREGATION)) {
            throw new TagMissingProtocolException(Tag.TYPE_AGGREGATION);
        }
        try {
            put(Tag.TYPE_AGGREGATION, new Integer(message.get(Tag.TYPE_AGGREGATION)));
        } catch (NumberFormatException exception) {
            throw new NotParsingValueProtocolException(Tag.TYPE_AGGREGATION);
        }
    }

    public LogonClientMessage(String clientID, Integer typeAggregation) {
        put(Tag.MSG_TYPE, MsgType.LOGON);
        put(Tag.CLIENT_ID, clientID);
        put(Tag.TYPE_AGGREGATION, typeAggregation);
    }

}
